using UnityEngine;
using System.Collections;

public class BSP_Nodes
{
	public BSP_Nodes parent;
	public BSP_Nodes rightChild;
	public BSP_Nodes leftChild;
	public BSP_Rectangle[] rooms = new BSP_Rectangle[100];
	public BSP_Rectangle[] rectangles = new BSP_Rectangle[100];
	public BSP_Nodes currentNode;
	public BSP_Rectangle myRectangle;
	public BSP_Nodes root;
	public Line splitLine;
	public int childi=0;
	public int roomi=0;
	public int idi=0;
	public int id;
	public BSP_Nodes sister;
	private int x;
	private int y;
	private int width;
	private int height;
	
	public void Start()
	{
		rooms[0] = new BSP_Rectangle();
		rectangles[0] = new BSP_Rectangle();
		currentNode = new BSP_Nodes();
		this.root = this.currentNode;
		this.leftChild = new BSP_Nodes();
		this.rightChild = new BSP_Nodes();
	}
		
	public BSP_Nodes()
	{
		this.parent = null;
		this.sister = null;
		this.x = 0;
		this.y = 0;
		this.width = 1;
		this.height = 1;
	}
	public BSP_Nodes(int newX, int newY, int w, int h)
	{
		this.parent = null;
		this.sister = null;
		this.x = newX;
		this.y = newY;
		this.width = w;
		this.height = h;
	}
	
	public void addRoom(BSP_Rectangle newRoom)
	{
		if(this.parent != null)	this.parent.addRoom(newRoom);
		this.rooms[roomi] = newRoom;
		roomi++;
	}
	
	public void setCurrent()
	{
		this.currentNode = new BSP_Nodes();
		this.currentNode.id = idi;
		roomi++;
		idi++;
	}
	
	public void addRectangle(BSP_Rectangle newRect)
	{
		int a=0;
		if(this.rectangles.Length == 0)
		{
			this.myRectangle = newRect;
		}
		this.rectangles[a] = newRect;
		if(this.parent != null)
			this.parent.addRectangle(newRect);
		a++;
	}
	
	/*public void createChildren(BSP_Nodes currentNode)
	{
		if(this.parent == null)
		{
			this.setCurrent();
			this.root = currentNode;
			this.setCurrent();
			this.root.rightChild[childi] = new BSP_Nodes();
			this.setCurrent();
			this.leftChild[childi] = currentNode;
			this.setCurrent();
			this.rightChild[childi].parent = root;
			this.leftChild[childi].parent = root;
			this.rightChild[childi].sister = leftChild[childi];
			this.leftChild[childi].sister = rightChild[childi];
		}
		else
		{
			this.rightChild[childi].parent = this.currentNode; 
			this.leftChild[childi].parent = this.currentNode;
			this.leftChild[childi].sister = currentNode;
			this.currentNode = this.rightChild[childi];
			this.setCurrent();
			this.rightChild[childi].sister = currentNode;
			this.currentNode = this.leftChild[childi];
			this.setCurrent();
			childi++;
		}
	}*/
	public int getem(char whichOne)
	{
		if(whichOne == 'w')
			return this.width;
		else if(whichOne == 'x')
			return this.x;
		else if(whichOne == 'y')
			return this.y;
		else if(whichOne == 'h')
			return this.height;
		else
			return this.roomi;
	}
	public bool contains(BSP_Rectangle other)
	{
	    bool pointOne = false;
        bool pointTwo = false;
        bool pointThree = false;
        bool pointFour = false;
        
        if(other.x > this.x &&
           other.x < this.x + this.width &&
           other.y > this.y &&
           other.y < this.y + this.height) pointOne = true;
           
        if(other.x + other.width > this.x &&
           other.x + other.width < this.x + this.width &&
           other.y > this.y &&
           other.y < this.y + this.height) pointTwo = true;
           
                      
        if(other.x > this.x &&
           other.x < this.x + this.width &&
           other.y + other.height > this.y &&
           other.y + other.height < this.y + this.height) pointThree = true;  
                
           
        if(other.x + other.width > this.x &&
           other.x + other.width < this.x + this.width &&
           other.y + other.height > this.y &&
           other.y + other.height < this.y + this.height) pointFour = true;
           
           
        return pointOne && pointTwo && pointThree && pointFour;
	}
}


